package cn.tedu.blog.server.controller.system;

import cn.tedu.blog.server.pojo.dto.ArticleAddNewDTO;
import cn.tedu.blog.server.pojo.dto.ArticleEditDTO;
import cn.tedu.blog.server.pojo.vo.ArticleAdminDetailVO;
import cn.tedu.blog.server.pojo.vo.ArticleDetailVO;
import cn.tedu.blog.server.service.IArticleService;
import cn.tedu.blog.server.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@Api(tags = "文章管理模块")
@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    IArticleService articleService;
    @ApiOperation("添加文章")
    @ApiModelProperty("100")
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody ArticleAddNewDTO addNewDTO) {
        log.debug("从请求中获取到的addNewDTO:{}", addNewDTO);
        articleService.addNew(addNewDTO);
        return JsonResult.ok();
    }
    @ApiModelProperty("200")
    @ApiOperation("删除文章")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable long id) {
        log.debug("接收到删除文章的请求, 参数id={}", id);
        articleService.deleteById(id);
        return JsonResult.ok();
    }
    @ApiModelProperty("300")
    @ApiOperation("批量删除文章")
    @PostMapping("/deleteByIds")
    public JsonResult deleteByIds(@RequestBody Long[]  ids) {
            log.debug("接收到根据多个id批量删除文章的请求");
        int rows = articleService.deleteByIds(ids);
        String message = "成功删除" + rows+ "条文章";
        return JsonResult.ok(message);
    }
    @ApiModelProperty("400")
    @ApiOperation("修改文章")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@PathVariable long id, @RequestBody ArticleEditDTO articleEditDTO) {
        log.debug("接收到修改文章的请求{}",articleEditDTO);
        articleService.update(id, articleEditDTO);
        return JsonResult.ok();
    }
    @ApiOperation("查询文章")
    @ApiModelProperty("500")
    @PostMapping("/adminSelectFind")
    public JsonResult adminSelectFind(@RequestBody ArticleAdminDetailVO articleAdminDetailVO) {
        log.debug("=====正在执行后台文章列表查询====={}", articleAdminDetailVO.toString());
        List<ArticleAdminDetailVO> list = articleService.adminSelectFindAll(articleAdminDetailVO);
        return JsonResult.ok(list);
    }
    @ApiOperation("根据id查询文章")
    @ApiModelProperty("600")
    @GetMapping("/{id:[0-9]+}/selectById")
    public JsonResult articleSelectById(@PathVariable Long id){
        log.error("传入id:{}",id);
        ArticleDetailVO articleDetailVO= articleService.adminSelect(id);
        return JsonResult.ok(articleDetailVO);
    }
    @ApiOperation("查询文章总数")
    @ApiModelProperty("700")
    @GetMapping("/selectCount")
    public JsonResult selectCount(){
       int count= articleService.selectCount();
       return JsonResult.ok(count);
    }
}
